//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System.Web.UI.HtmlControls;
using CommunityServer.Galleries.Controls;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;
using CommunityServer.Components;

namespace CommunityServer.Galleries.Controls
{
	/// <summary>
	/// Summary description for GalleryLinks.
	/// </summary>
	public class GalleryLinks : GalleryThemedControl
	{

		#region Public Properties

		private GalleryPost dataSource;
		public GalleryPost DataSource
		{
			get { return dataSource; }
			set { dataSource = value; }
		}

		#endregion

		#region Child Controls

		private ITrackedThread TrackingButton;
		private HtmlGenericControl TrackingButtonItem;

		#endregion

		#region Skin

	    protected override void AttachChildControls()
	    {
			TrackingButton =  FindControl( "TrackingButton" ) as ITrackedThread;
			TrackingButtonItem = (HtmlGenericControl) FindControl( "TrackingButtonItem" );
		}

		#endregion

		#region Event Overrides

		protected override void OnLoad(System.EventArgs e)
		{
			base.OnLoad(e);
			this.DataBind();
		}
		
		public override void DataBind()
		{
			base.DataBind();
			BindData();

		}

		#endregion

		private void BindData()
		{
			if(TrackingButton != null)
			{
				CSContext cSContext = CSContext.Current;
				if(DataSource == null && cSContext.PostID != -1 && !cSContext.User.IsAnonymous)
					DataSource = GalleryPosts.GetPicture(cSContext.PostID); //return;
				
				if(DataSource != null)
				{
                    TrackingButton.IsTracked = DataSource.IsTracked = ThreadTracking.IsTracked(DataSource.ThreadID, CurrentUser.UserID);
					TrackingButton.ThreadID = DataSource.ThreadID;
				}
				else
				{
					TrackingButton.Visible = false;
					if(TrackingButtonItem != null)
						TrackingButtonItem.Visible = false;
				}
			}

		}


	}
}
